package com.sunlg.smarthome.ui.guard;

import android.support.v7.app.ActionBar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.contract.SecurityDeviceContract;
import com.sunlg.smarthome.contract.presenter.SecurityDevicePresenter;
import com.sunlg.smarthome.data.entity.SecurityDevice;
import com.sunlg.smarthome.ui.wedget.RecyclerViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by sunlg on 2017/5/24.
 */

public class SecurityDeviceListActivity extends BaseActivity implements SecurityDeviceContract.View{

    @BindView(R.id.rv_security_device_list)
    RecyclerView rvSecurityDeviceList;
    @BindView(R.id.tv_none_device)
    TextView tvNoneDevice;

    private List<SecurityDevice> securityDevices;
    private SecurityDeviceAdapter adapter;
    private SecurityDeviceContract.Presenter presenter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_security_device_list;
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        super.setupToolBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("安防控制");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doFinish();
            }
        });
    }

    @Override
    protected void init() {
        securityDevices = new ArrayList<>();
        adapter = new SecurityDeviceAdapter(this, securityDevices);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        rvSecurityDeviceList.setLayoutManager(layoutManager);
        rvSecurityDeviceList.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.VERTICAL));
        rvSecurityDeviceList.setAdapter(adapter);

        presenter = new SecurityDevicePresenter();
        presenter.attachView(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        presenter.loadAllSecurityDevice();
    }

    @Override
    protected void initView() {
        super.initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.action_add){
            overlay(AddSecurityDeviceActivity.class);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showNoneDeviceText() {
        if(tvNoneDevice.getVisibility() != View.VISIBLE){
            tvNoneDevice.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void hideNoneDeviceText() {
        if(tvNoneDevice.getVisibility() == View.VISIBLE){
            tvNoneDevice.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showSecurityDeviceList(List<SecurityDevice> deviceList) {
        securityDevices.clear();
        securityDevices.addAll(deviceList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
    }
}
